1 #property copyright "Copyright © 2010, Pix"
\r
2 extern string ST1 = "Lot Decimal Places e.g: 0.1 = 1, 0.01 = 2";
\r
3 extern int lot_st = 2;
\r
4 extern string ST2 = "Lot size";
\r
5 extern double Lots = 0.1;
\r
6 extern double LotExponent = 1.667;
\r
7 extern double TakeProfit = 10;
\r
8 extern double Stoploss = 500;
\r
9 extern bool UseTrailingStop = false;
\r
10 extern double TrailStart = 10;
\r
11 extern double TrailStop = 10;
\r
12 extern double PipStep = 30;
\r
15 extern int MaxTrades = 10;
\r
16 extern bool UseEquityStop = false;
\r
17 extern double TotalEquityRisk = 20; //loss as a percentage of equity
\r
18 extern bool UseTimeOut = false;
\r
19 extern double MaxTradeOpenHours = 48;
\r
20 extern double slip = 3;
\r
22 extern int MagicNumber = 88824;
\r
23 double PriceTarget, StartEquity, BuyTarget, SellTarget;
\r
24 double AveragePrice, SellLimit, BuyLimit;
\r
25 double LastBuyPrice, LastSellPrice, ClosePrice, Spread;
\r
27 extern string EAName = "G.P.Morgan-KS";
\r
28 datetime timeprev=0, expiration;
\r
33 bool TradeNow = false, LongTrade=false, ShortTrade=false;
\r
35 bool NewOrdersPlaced = false;// established order
\r
39 Spread = MarketInfo(Symbol(), MODE_SPREAD)*Point;
\r
51 if (UseTrailingStop) TrailingAlls(TrailStart, TrailStop, AveragePrice); //then
\r
52 if (UseTimeOut) // default false
\r
53 if(CurTime() >= expiration) //then
\r
55 CloseThisSymbolAll();
\r
56 Print("Closed All due to TimeOut");
\r
59 if(timeprev==Time[0])
\r
63 double CurrentPairProfit = CalculateProfit(); //calculation of the current profit
\r
64 if(UseEquityStop)//external parameter - such as "stop loss"
\r
65 if(CurrentPairProfit<0 && MathAbs(CurrentPairProfit)>(TotalEquityRisk/100)*AccountEquityHigh())
\r
67 CloseThisSymbolAll();
\r
68 Print("Closed All due to Stop Out");
\r
69 NewOrdersPlaced = false;
\r
72 int total=OrdersTotal();
\r
74 for(int pos=0;pos<total;pos++)
\r
76 if(OrderSelect(pos,SELECT_BY_POS,MODE_TRADES)==false) continue;
\r
80 total=CountTrades(); // number of deals with a specific identification number (open)
\r
81 if (total == 0) flag = 0;
\r
82 for(cnt=OrdersTotal()-1;cnt>=0;cnt--)//test - in which direction is open transaction
\r
84 OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
\r
86 if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
\r
89 if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
\r
90 if(OrderType()==OP_BUY)
\r
92 if(Insure1!=0||Insure2!=0)
\r
93 if(Insure1==count||Insure2==count)
\r
100 LongTrade = false;
\r
107 ShortTrade = false;
\r
111 if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
\r
112 if(OrderType()==OP_SELL)
\r
114 if(Insure1!=0||Insure2!=0)
\r
115 if(Insure1==count||Insure2==count)
\r
123 ShortTrade = false;
\r
135 if(total>0 && total <= MaxTrades)//if the schedule had gone more / less PipStep then open the transaction
\r
138 LastBuyPrice = FindLastBuyPrice();
\r
139 LastSellPrice = FindLastSellPrice();
\r
140 if(LongTrade && (LastBuyPrice - Ask) >= (PipStep*Point))
\r
144 if(ShortTrade && (Bid - LastSellPrice) >= (PipStep*Point))
\r
152 ShortTrade = false;
\r
155 StartEquity = AccountEquity();//Returns the amount of own funds for the current account. The calculation of equity depends on the settings of the trading server.
\r
158 if (TradeNow) //if the schedule more PipStep jerked open transaction in the opposite direction
\r
160 LastBuyPrice = FindLastBuyPrice();
\r
161 LastSellPrice = FindLastSellPrice();
\r
164 NumOfTrades = total;
\r
165 iLots = NormalizeDouble(Lots*MathPow(LotExponent,NumOfTrades),lot_st);
\r
167 ticket = OpenPendingOrder(OP_SELL,iLots,Bid,slip,Ask,0,0,EAName+"-"+NumOfTrades,MagicNumber,0,HotPink);
\r
168 if(ticket<0){Print("Error: ",GetLastError()); return(0);}
\r
169 LastSellPrice = FindLastSellPrice();
\r
171 NewOrdersPlaced = true;
\r
173 else if (LongTrade)
\r
175 NumOfTrades = total;
\r
176 iLots = NormalizeDouble(Lots*MathPow(LotExponent,NumOfTrades),lot_st);
\r
177 ticket = OpenPendingOrder(OP_BUY,iLots,Ask,slip,Bid,0,0,EAName+"-"+NumOfTrades,MagicNumber,0,Lime);
\r
178 if(ticket<0){Print("Error: ",GetLastError()); return(0);}
\r
179 LastBuyPrice = FindLastBuyPrice();
\r
181 NewOrdersPlaced = true;
\r
185 if (TradeNow && total<1) //If there is no open transactions
\r
188 double PrevCl = iClose(Symbol(),0,2);//Returns the closing price
\r
189 double CurrCl = iClose(Symbol(),0,1);
\r
193 if (!ShortTrade && !LongTrade)
\r
195 NumOfTrades = total;
\r
196 iLots = NormalizeDouble(Lots*MathPow(LotExponent,NumOfTrades),lot_st);
\r
197 if(PrevCl > CurrCl)
\r
199 ticket = OpenPendingOrder(OP_SELL,iLots,SellLimit,slip,SellLimit,0,0,EAName+"-"+NumOfTrades,MagicNumber,0,HotPink);
\r
200 if(ticket<0){Print("Error: ",GetLastError()); return(0);}
\r
201 LastBuyPrice = FindLastBuyPrice();
\r
202 NewOrdersPlaced = true;
\r
206 ticket = OpenPendingOrder(OP_BUY,iLots,BuyLimit,slip,BuyLimit,0,0,EAName+"-"+NumOfTrades,MagicNumber,0,Lime);
\r
207 if(ticket<0){Print("Error: ",GetLastError()); return(0);}
\r
208 LastSellPrice = FindLastSellPrice();
\r
209 NewOrdersPlaced = true;
\r
211 if(ticket>0) expiration = CurTime()+MaxTradeOpenHours*60*60;
\r
216 //----------------------- CALCULATE AVERAGE OPENING PRICE
\r
217 total=CountTrades();
\r
220 for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
\r
222 OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
\r
223 if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
\r
225 if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
\r
226 if(OrderType()==OP_BUY || OrderType()==OP_SELL)
\r
228 AveragePrice=AveragePrice+OrderOpenPrice()*OrderLots();
\r
229 Count = Count + OrderLots();
\r
233 AveragePrice=NormalizeDouble(AveragePrice/Count, Digits);
\r
236 //----------------------- RECALCULATE STOPLOSS & PROFIT TARGET BASED ON AVERAGE OPENING PRICE
\r
237 if(NewOrdersPlaced)
\r
238 for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
\r
240 OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
\r
241 if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
\r
243 if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
\r
244 if(OrderType()==OP_BUY) // Calculate profit/stop target for long
\r
246 PriceTarget=AveragePrice+(TakeProfit*Point);
\r
247 BuyTarget = PriceTarget;
\r
248 Stopper=AveragePrice-(Stoploss*Point);
\r
252 if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
\r
253 if(OrderType()==OP_SELL) // Calculate profit/stop target for short
\r
255 PriceTarget=AveragePrice-(TakeProfit*Point);
\r
256 SellTarget = PriceTarget;
\r
257 Stopper=AveragePrice+(Stoploss*Point);
\r
262 //----------------------- IF NEEDED CHANGE ALL OPEN ORDERS TO NEWLY CALCULATED PROFIT TARGET
\r
263 if(NewOrdersPlaced)
\r
264 if(flag==1)// check if average has really changed
\r
266 for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
\r
268 // PriceTarget=total;
\r
269 OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
\r
270 if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
\r
272 if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
\r
273 // OrderModify(OrderTicket(),0,Stopper,PriceTarget,0,Yellow);// set all positions to averaged levels
\r
274 OrderModify(OrderTicket(),AveragePrice,OrderStopLoss(),PriceTarget,0,Yellow);// set all positions to averaged levels
\r
275 NewOrdersPlaced = false;
\r
284 for(trade=OrdersTotal()-1;trade>=0;trade--)
\r
286 OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
\r
288 if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
\r
291 if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
\r
292 if(OrderType()==OP_SELL || OrderType()==OP_BUY)
\r
299 void CloseThisSymbolAll()
\r
302 for(trade=OrdersTotal()-1;trade>=0;trade--)
\r
304 OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
\r
306 if(OrderSymbol()!=Symbol())
\r
308 if(OrderSymbol()==Symbol() && OrderMagicNumber()== MagicNumber)
\r
310 if(OrderType()==OP_BUY)
\r
311 OrderClose(OrderTicket(),OrderLots(),Bid,slip,Blue);
\r
313 if(OrderType()==OP_SELL)
\r
314 OrderClose(OrderTicket(),OrderLots(),Ask,slip,Red);
\r
320 int OpenPendingOrder(int pType,double pLots,double pLevel,int sp, double pr, int sl, int tp,string pComment,int pMagic,datetime pExpiration,color pColor)
\r
325 int NumberOfTries = 100;
\r
329 for(c = 0 ; c < NumberOfTries ; c++)
\r
331 ticket=OrderSend(Symbol(),OP_BUYLIMIT,pLots,pLevel,sp,StopLong(pr,sl),TakeLong(pLevel,tp),pComment,pMagic,pExpiration,pColor);
\r
332 err=GetLastError();
\r
339 if(err==4 || err==137 ||err==146 || err==136) //Busy errors
\r
344 else //normal error
\r
352 for(c = 0 ; c < NumberOfTries ; c++)
\r
354 ticket=OrderSend(Symbol(),OP_BUYSTOP,pLots,pLevel,sp,StopLong(pr,sl),TakeLong(pLevel,tp),pComment,pMagic,pExpiration,pColor);
\r
355 err=GetLastError();
\r
362 if(err==4 || err==137 ||err==146 || err==136) //Busy errors
\r
367 else //normal error
\r
375 for(c = 0 ; c < NumberOfTries ; c++)
\r
378 ticket=OrderSend(Symbol(),OP_BUY,pLots,Ask,sp,StopLong(Bid,sl),TakeLong(Ask,tp),pComment,pMagic,pExpiration,pColor);
\r
379 err=GetLastError();
\r
386 if(err==4 || err==137 ||err==146 || err==136) //Busy errors
\r
391 else //normal error
\r
399 for(c = 0 ; c < NumberOfTries ; c++)
\r
401 ticket=OrderSend(Symbol(),OP_SELLLIMIT,pLots,pLevel,sp,StopShort(pr,sl),TakeShort(pLevel,tp),pComment,pMagic,pExpiration,pColor);
\r
402 err=GetLastError();
\r
409 if(err==4 || err==137 ||err==146 || err==136) //Busy errors
\r
414 else //normal error
\r
422 for(c = 0 ; c < NumberOfTries ; c++)
\r
424 ticket=OrderSend(Symbol(),OP_SELLSTOP,pLots,pLevel,sp,StopShort(pr,sl),TakeShort(pLevel,tp),pComment,pMagic,pExpiration,pColor);
\r
425 err=GetLastError();
\r
432 if(err==4 || err==137 ||err==146 || err==136) //Busy errors
\r
437 else //normal error
\r
445 for(c = 0 ; c < NumberOfTries ; c++)
\r
447 ticket=OrderSend(Symbol(),OP_SELL,pLots,Bid,sp,StopShort(Ask,sl),TakeShort(Bid,tp),pComment,pMagic,pExpiration,pColor);
\r
448 err=GetLastError();
\r
455 if(err==4 || err==137 ||err==146 || err==136) //Busy errors
\r
460 else //normal error
\r
472 double StopLong(double price,int stop)
\r
477 return(price-(stop*Point));
\r
480 double StopShort(double price,int stop)
\r
485 return(price+(stop*Point));
\r
488 double TakeLong(double price,int take)
\r
493 return(price+(take*Point));
\r
496 double TakeShort(double price,int take)
\r
501 return(price-(take*Point));
\r
505 double CalculateProfit()
\r
509 for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
\r
511 OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
\r
512 if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
\r
514 if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
\r
515 if(OrderType()==OP_BUY || OrderType()==OP_SELL)
\r
517 Profit=Profit+OrderProfit();
\r
524 void TrailingAlls(int start,int stop, double AvgPrice)
\r
534 for(trade=OrdersTotal()-1;trade>=0;trade--)
\r
536 if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
\r
539 if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
\r
542 if(OrderSymbol()==Symbol()||OrderMagicNumber()==MagicNumber)
\r
544 if(OrderType()==OP_BUY)
\r
546 profit=NormalizeDouble((Bid-AvgPrice)/Point,0);
\r
549 stoptrade=OrderStopLoss();
\r
550 stopcal=Bid-(stop*Point);
\r
551 if(stoptrade==0||(stoptrade!=0&&stopcal>stoptrade))
\r
552 // OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Blue);
\r
553 OrderModify(OrderTicket(),AvgPrice,stopcal,OrderTakeProfit(),0,Aqua);
\r
556 if(OrderType()==OP_SELL)
\r
558 profit=NormalizeDouble((AvgPrice-Ask)/Point,0);
\r
561 stoptrade=OrderStopLoss();
\r
562 stopcal=Ask+(stop*Point);
\r
563 if(stoptrade==0||(stoptrade!=0&&stopcal<stoptrade))
\r
564 // OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Red);
\r
565 OrderModify(OrderTicket(),AvgPrice,stopcal,OrderTakeProfit(),0,Red);
\r
575 double AccountEquityHigh()
\r
577 static double AccountEquityHighAmt,PrevEquity;
\r
578 if(CountTrades()==0) AccountEquityHighAmt=AccountEquity();
\r
579 if(AccountEquityHighAmt < PrevEquity) AccountEquityHighAmt=PrevEquity;
\r
580 else AccountEquityHighAmt=AccountEquity();
\r
581 PrevEquity = AccountEquity();
\r
582 return(AccountEquityHighAmt);
\r
586 double FindLastBuyPrice()//returns the number of the last transaction buy
\r
588 double oldorderopenprice = 0, orderprice;
\r
589 int cnt, oldticketnumber = 0, ticketnumber;
\r
591 for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
\r
593 OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
\r
594 if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
\r
596 if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==OP_BUY)
\r
598 ticketnumber = OrderTicket();
\r
599 if(ticketnumber>oldticketnumber)
\r
601 orderprice=OrderOpenPrice();
\r
602 oldorderopenprice=orderprice;
\r
603 oldticketnumber=ticketnumber;
\r
608 return(orderprice);
\r
611 double FindLastSellPrice()
\r
613 double oldorderopenprice = 0, orderprice;
\r
614 int cnt, oldticketnumber = 0, ticketnumber;
\r
616 for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
\r
618 OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
\r
619 if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
\r
621 if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==OP_SELL)
\r
623 ticketnumber = OrderTicket();
\r
624 if(ticketnumber>oldticketnumber)
\r
626 orderprice=OrderOpenPrice();
\r
627 oldorderopenprice=orderprice;
\r
628 oldticketnumber=ticketnumber;
\r
633 return(orderprice);
\r